{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "53532995",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa67d481",
   "metadata": {},
   "source": [
    "# 导入数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b99d71c",
   "metadata": {},
   "source": [
    "## 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "52eeec61",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"车贷违约预测.csv\",engine=\"python\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "226b9b5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>出生日期</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>1981</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>1967</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>1977</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>1995</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>1974</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 49 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本  贷款与资产比列   品牌  骑车销售商  车厂  出生日期  货款日期  地区  ...  \\\n",
       "0  601758  65532  78990    84.38  136  20490  45  1981  2018   8  ...   \n",
       "1  519488  56759  65325    89.55   61  22778  86  1967  2018   6  ...   \n",
       "2  447579  58413  67960    89.02    5  15663  86  1977  2018   9  ...   \n",
       "3  648134  72317  99750    73.68   76  17242  48  1995  2018   8  ...   \n",
       "4  458210  50078  65450    79.45  146  14181  45  1974  2018  17  ...   \n",
       "\n",
       "   尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0           0        0        0       0       1.00        0        0   \n",
       "1     2054139  2036500  2036500   34455       0.99       59        0   \n",
       "2           0        0        0       0       1.00        0        0   \n",
       "3           0    13813    13813       0   13814.00    13813        0   \n",
       "4      467161   550000   550000   12863       1.18       42        0   \n",
       "\n",
       "   贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0         1.0            1.00     0  \n",
       "1         1.0            1.33     1  \n",
       "2         1.0            1.00     1  \n",
       "3         1.0            2.00     0  \n",
       "4         1.0            1.06     1  \n",
       "\n",
       "[5 rows x 49 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff051107",
   "metadata": {},
   "source": [
    "## 异常值处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "979a562d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['客户编号', '已发货款', '资产成本', '贷款与资产比列', '品牌', '骑车销售商', '车厂', '出生日期', '货款日期',\n",
       "       '地区', '对接员工编号', '是否填写手机号', '受否填写身份证', '是否出具驾驶证', '是否填写护照', '信用评分',\n",
       "       '主账户贷款次数', '主账户有效贷款次数', '主账户中尚未还清有效贷款', '主账户中已批准的贷款', '主账户中已发放贷款',\n",
       "       '次账户贷款次数', '次账户有效贷款次数', '次账户中尚未还清有效贷款', '次账户中已批准贷款', '次账户中已发放贷款',\n",
       "       '主账户每月还款', '次账户没用还款', '近六个月新贷款次数', '近六个月违约次数', '平均贷款期限', '第一次贷款距今时间',\n",
       "       '贷款查询次数', '是否违约', '贷款与资产比', '贷款总次数', '主账户无效贷款次数', '次账户无效贷款次数',\n",
       "       '无效贷款总次数', '尚未还清有效贷款总额', '已批准贷款总额', '已发放贷款总额', '每月还款总额', '贷款与已还贷款比列',\n",
       "       '主账户还款期数', '次账户还款期数', '贷款与已批准贷款比列', '总贷款次数与总有效贷款次数比', '工作类型'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1f9725e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除前两列数据\n",
    "data.drop([\"出生日期\",\"对接员工编号\"],axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "366e9789",
   "metadata": {},
   "source": [
    "## 自变量选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8ca899e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    164289\n",
       "1     35428\n",
       "Name: 是否违约, dtype: int64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#自变量选择\n",
    "data.是否违约.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e7f20940",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>是否填写手机号</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.38</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.55</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.02</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.68</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.45</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199712</th>\n",
       "      <td>436304</td>\n",
       "      <td>36439</td>\n",
       "      <td>60424</td>\n",
       "      <td>62.89</td>\n",
       "      <td>10</td>\n",
       "      <td>23507</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>592668</td>\n",
       "      <td>525000</td>\n",
       "      <td>525000</td>\n",
       "      <td>0</td>\n",
       "      <td>0.89</td>\n",
       "      <td>525000</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199713</th>\n",
       "      <td>598007</td>\n",
       "      <td>52303</td>\n",
       "      <td>72677</td>\n",
       "      <td>72.93</td>\n",
       "      <td>34</td>\n",
       "      <td>15142</td>\n",
       "      <td>86</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199714</th>\n",
       "      <td>466468</td>\n",
       "      <td>54413</td>\n",
       "      <td>62710</td>\n",
       "      <td>89.30</td>\n",
       "      <td>67</td>\n",
       "      <td>16565</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1185601</td>\n",
       "      <td>1220000</td>\n",
       "      <td>1220000</td>\n",
       "      <td>2500</td>\n",
       "      <td>1.03</td>\n",
       "      <td>487</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199715</th>\n",
       "      <td>634336</td>\n",
       "      <td>54509</td>\n",
       "      <td>71921</td>\n",
       "      <td>77.86</td>\n",
       "      <td>74</td>\n",
       "      <td>16846</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>199716</th>\n",
       "      <td>638308</td>\n",
       "      <td>63147</td>\n",
       "      <td>72000</td>\n",
       "      <td>89.58</td>\n",
       "      <td>2</td>\n",
       "      <td>23169</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>97963</td>\n",
       "      <td>106508</td>\n",
       "      <td>106508</td>\n",
       "      <td>0</td>\n",
       "      <td>1.09</td>\n",
       "      <td>106508</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>199717 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          客户编号   已发货款   资产成本  贷款与资产比列   品牌  骑车销售商  车厂  货款日期  地区  是否填写手机号  ...  \\\n",
       "0       601758  65532  78990    84.38  136  20490  45  2018   8        1  ...   \n",
       "1       519488  56759  65325    89.55   61  22778  86  2018   6        1  ...   \n",
       "2       447579  58413  67960    89.02    5  15663  86  2018   9        1  ...   \n",
       "3       648134  72317  99750    73.68   76  17242  48  2018   8        1  ...   \n",
       "4       458210  50078  65450    79.45  146  14181  45  2018  17        1  ...   \n",
       "...        ...    ...    ...      ...  ...    ...  ..   ...  ..      ...  ...   \n",
       "199712  436304  36439  60424    62.89   10  23507  45  2018   3        1  ...   \n",
       "199713  598007  52303  72677    72.93   34  15142  86  2018   6        1  ...   \n",
       "199714  466468  54413  62710    89.30   67  16565  45  2018   6        1  ...   \n",
       "199715  634336  54509  71921    77.86   74  16846  45  2018   4        1  ...   \n",
       "199716  638308  63147  72000    89.58    2  23169  45  2018   4        1  ...   \n",
       "\n",
       "        尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0                0        0        0       0       1.00        0        0   \n",
       "1          2054139  2036500  2036500   34455       0.99       59        0   \n",
       "2                0        0        0       0       1.00        0        0   \n",
       "3                0    13813    13813       0   13814.00    13813        0   \n",
       "4           467161   550000   550000   12863       1.18       42        0   \n",
       "...            ...      ...      ...     ...        ...      ...      ...   \n",
       "199712      592668   525000   525000       0       0.89   525000        0   \n",
       "199713           0        0        0       0       1.00        0        0   \n",
       "199714     1185601  1220000  1220000    2500       1.03      487        0   \n",
       "199715           0        0        0       0       1.00        0        0   \n",
       "199716       97963   106508   106508       0       1.09   106508        0   \n",
       "\n",
       "        贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0              1.0            1.00     0  \n",
       "1              1.0            1.33     1  \n",
       "2              1.0            1.00     1  \n",
       "3              1.0            2.00     0  \n",
       "4              1.0            1.06     1  \n",
       "...            ...             ...   ...  \n",
       "199712         1.0            3.00     0  \n",
       "199713         1.0            1.00     0  \n",
       "199714         1.0            3.00     1  \n",
       "199715         1.0            1.00     1  \n",
       "199716         1.0            2.00     2  \n",
       "\n",
       "[199717 rows x 47 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.loc[~((data.是否违约 ==\"bad_ind\") | (data.是否违约.isna())) ,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f50e980f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择自变量\n",
    "# 自变量相关性\n",
    "from  scipy  import stats\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "31bb71a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "68000     575\n",
       "67000     503\n",
       "72000     474\n",
       "70000     432\n",
       "66000     407\n",
       "         ... \n",
       "82196       1\n",
       "169480      1\n",
       "52631       1\n",
       "54578       1\n",
       "60424       1\n",
       "Name: 资产成本, Length: 43627, dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.资产成本.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "bd512c3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "crosstable = pd.crosstab(data.是否违约,data.资产成本)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "115309bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(46858.382514045916,\n",
       " 5.9148177002807146e-27,\n",
       " 43626,\n",
       " array([[1.64521798, 0.82260899, 0.82260899, ..., 0.82260899, 0.82260899,\n",
       "         0.82260899],\n",
       "        [0.35478202, 0.17739101, 0.17739101, ..., 0.17739101, 0.17739101,\n",
       "         0.17739101]]))"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats.chi2_contingency(crosstable) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c7446b70",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      0\n",
       "1    300\n",
       "2      0\n",
       "3    763\n",
       "4    379\n",
       "Name: 信用评分, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.信用评分.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d2e81dba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2774.3510217293656,\n",
       " 2.174780140697608e-285,\n",
       " 571,\n",
       " array([[8.20075356e+04, 2.46782698e+00, 6.84410681e+02, ...,\n",
       "         4.77113215e+01, 8.22608992e-01, 3.29043597e+00],\n",
       "        [1.76844644e+04, 5.32173025e-01, 1.47589319e+02, ...,\n",
       "         1.02886785e+01, 1.77391008e-01, 7.09564033e-01]]))"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "crosstable = pd.crosstab(data.是否违约,data.信用评分)\n",
    "stats.chi2_contingency(crosstable) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "dec4634d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2171.201249470168,\n",
       " 0.0,\n",
       " 81,\n",
       " array([[4080.96320794, 9313.57900429, 6613.77629346, 6408.94665452,\n",
       "         2289.32082397, 2285.20777901, 1780.94846708, 2885.71234297,\n",
       "         3199.9489778 , 2077.0877041 , 1104.76387588, 2144.54164142,\n",
       "         4488.15465884,  825.0768187 , 3528.1699655 , 4150.88497224,\n",
       "         3138.25330342, 1100.65083093, 5544.38460421,  476.29060621,\n",
       "         6137.48568725, 1907.63025181,  419.53058578, 3320.0498906 ,\n",
       "         3471.40994507,  482.04886915, 1400.9031129 , 1144.24910749,\n",
       "         2449.72957735,  217.16877381, 8029.48636821, 1624.65275865,\n",
       "          574.18107622, 1243.78479549,  910.62815384, 1012.63166881,\n",
       "         3001.7002108 ,  604.61760892, 1026.61602167, 1524.29446166,\n",
       "         2413.53478172,  731.29939364,  107.76177792, 1291.49611701,\n",
       "          270.63835828,  245.13747953,  259.12183239, 2740.93316042,\n",
       "         1171.39520421, 1709.3814848 ,   64.16350135,  393.20709804,\n",
       "         2936.71410045,  628.47326968,  753.50983642, 2284.38517002,\n",
       "         5526.2872064 , 3057.63762224,  337.26968661, 3705.03089872,\n",
       "         2883.24451599, 3548.73519029,  479.58104217,   43.59827656,\n",
       "         1908.4528608 , 2492.50524492, 1949.58331038,  714.84721381,\n",
       "         1423.93616467,  489.45235008,  128.32700271, 1274.22132818,\n",
       "          607.90804488, 1047.18124646, 2623.30007461, 1173.86303119,\n",
       "         1146.71693446,  885.12727509,  272.28357626,  248.4279155 ,\n",
       "          260.76705038,  120.10091279],\n",
       "        [ 880.03679206, 2008.42099571, 1426.22370654, 1382.05334548,\n",
       "          493.67917603,  492.79222099,  384.05153292,  622.28765703,\n",
       "          690.0510222 ,  447.9122959 ,  238.23612412,  462.45835858,\n",
       "          967.84534116,  177.9231813 ,  760.8300345 ,  895.11502776,\n",
       "          676.74669658,  237.34916907, 1195.61539579,  102.70939379,\n",
       "         1323.51431275,  411.36974819,   90.46941422,  715.9501094 ,\n",
       "          748.59005493,  103.95113085,  302.0968871 ,  246.75089251,\n",
       "          528.27042265,   46.83122619, 1731.51363179,  350.34724135,\n",
       "          123.81892378,  268.21520451,  196.37184616,  218.36833119,\n",
       "          647.2997892 ,  130.38239108,  221.38397833,  328.70553834,\n",
       "          520.46521828,  157.70060636,   23.23822208,  278.50388299,\n",
       "           58.36164172,   52.86252047,   55.87816761,  591.06683958,\n",
       "          252.60479579,  368.6185152 ,   13.83649865,   84.79290196,\n",
       "          633.28589955,  135.52673032,  162.49016358,  492.61482998,\n",
       "         1191.7127936 ,  659.36237776,   72.73031339,  798.96910128,\n",
       "          621.75548401,  765.26480971,  103.41895783,    9.40172344,\n",
       "          411.5471392 ,  537.49475508,  420.41668962,  154.15278619,\n",
       "          307.06383533,  105.54764992,   27.67299729,  274.77867182,\n",
       "          131.09195512,  225.81875354,  565.69992539,  253.13696881,\n",
       "          247.28306554,  190.87272491,   58.71642374,   53.5720845 ,\n",
       "           56.23294962,   25.89908721]]))"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.品牌.value_counts()\n",
    "crosstable = pd.crosstab(data.是否违约,data.品牌)\n",
    "stats.chi2_contingency(crosstable) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f68bad4",
   "metadata": {},
   "source": [
    "## 查看是否有缺失"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f53dc156",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 199717 entries, 0 to 199716\n",
      "Data columns (total 47 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   客户编号            199717 non-null  int64  \n",
      " 1   已发货款            199717 non-null  int64  \n",
      " 2   资产成本            199717 non-null  int64  \n",
      " 3   贷款与资产比列         199717 non-null  float64\n",
      " 4   品牌              199717 non-null  int64  \n",
      " 5   骑车销售商           199717 non-null  int64  \n",
      " 6   车厂              199717 non-null  int64  \n",
      " 7   货款日期            199717 non-null  int64  \n",
      " 8   地区              199717 non-null  int64  \n",
      " 9   是否填写手机号         199717 non-null  int64  \n",
      " 10  受否填写身份证         199717 non-null  int64  \n",
      " 11  是否出具驾驶证         199717 non-null  int64  \n",
      " 12  是否填写护照          199717 non-null  int64  \n",
      " 13  信用评分            199717 non-null  int64  \n",
      " 14  主账户贷款次数         199717 non-null  int64  \n",
      " 15  主账户有效贷款次数       199717 non-null  int64  \n",
      " 16  主账户中尚未还清有效贷款    199717 non-null  int64  \n",
      " 17  主账户中已批准的贷款      199717 non-null  int64  \n",
      " 18  主账户中已发放贷款       199717 non-null  int64  \n",
      " 19  次账户贷款次数         199717 non-null  int64  \n",
      " 20  次账户有效贷款次数       199717 non-null  int64  \n",
      " 21  次账户中尚未还清有效贷款    199717 non-null  int64  \n",
      " 22  次账户中已批准贷款       199717 non-null  int64  \n",
      " 23  次账户中已发放贷款       199717 non-null  int64  \n",
      " 24  主账户每月还款         199717 non-null  int64  \n",
      " 25  次账户没用还款         199717 non-null  int64  \n",
      " 26  近六个月新贷款次数       199717 non-null  int64  \n",
      " 27  近六个月违约次数        199717 non-null  int64  \n",
      " 28  平均贷款期限          199717 non-null  int64  \n",
      " 29  第一次贷款距今时间       199717 non-null  int64  \n",
      " 30  贷款查询次数          199717 non-null  int64  \n",
      " 31  是否违约            199717 non-null  int64  \n",
      " 32  贷款与资产比          199717 non-null  float64\n",
      " 33  贷款总次数           199717 non-null  int64  \n",
      " 34  主账户无效贷款次数       199717 non-null  int64  \n",
      " 35  次账户无效贷款次数       199717 non-null  int64  \n",
      " 36  无效贷款总次数         199717 non-null  int64  \n",
      " 37  尚未还清有效贷款总额      199717 non-null  int64  \n",
      " 38  已批准贷款总额         199717 non-null  int64  \n",
      " 39  已发放贷款总额         199717 non-null  int64  \n",
      " 40  每月还款总额          199717 non-null  int64  \n",
      " 41  贷款与已还贷款比列       199717 non-null  float64\n",
      " 42  主账户还款期数         199717 non-null  int64  \n",
      " 43  次账户还款期数         199717 non-null  int64  \n",
      " 44  贷款与已批准贷款比列      199717 non-null  float64\n",
      " 45  总贷款次数与总有效贷款次数比  199717 non-null  float64\n",
      " 46  工作类型            199717 non-null  int64  \n",
      "dtypes: float64(5), int64(42)\n",
      "memory usage: 71.6 MB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>是否填写手机号</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.0</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.00</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>1.997170e+05</td>\n",
       "      <td>199717.000000</td>\n",
       "      <td>199717.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>535690.886665</td>\n",
       "      <td>54256.272280</td>\n",
       "      <td>7.582391e+04</td>\n",
       "      <td>74.643960</td>\n",
       "      <td>72.698508</td>\n",
       "      <td>19634.049665</td>\n",
       "      <td>69.085766</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>7.245222</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.743125e+05</td>\n",
       "      <td>2.299233e+05</td>\n",
       "      <td>2.294165e+05</td>\n",
       "      <td>1.344553e+04</td>\n",
       "      <td>inf</td>\n",
       "      <td>5.059582e+04</td>\n",
       "      <td>2.928000e+03</td>\n",
       "      <td>5.535709e+02</td>\n",
       "      <td>1.438913</td>\n",
       "      <td>0.487475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>68193.411418</td>\n",
       "      <td>12977.656996</td>\n",
       "      <td>1.892894e+04</td>\n",
       "      <td>11.490485</td>\n",
       "      <td>69.706185</td>\n",
       "      <td>3493.655400</td>\n",
       "      <td>22.128288</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.481338</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9.813640e+05</td>\n",
       "      <td>2.530977e+06</td>\n",
       "      <td>2.534185e+06</td>\n",
       "      <td>1.531618e+05</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.275670e+06</td>\n",
       "      <td>1.065410e+05</td>\n",
       "      <td>1.141343e+05</td>\n",
       "      <td>0.792213</td>\n",
       "      <td>0.561915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>417428.000000</td>\n",
       "      <td>13320.000000</td>\n",
       "      <td>3.700000e+04</td>\n",
       "      <td>10.030000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10524.000000</td>\n",
       "      <td>45.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>-6.678296e+06</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>-110000.33</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>476762.000000</td>\n",
       "      <td>46977.000000</td>\n",
       "      <td>6.571400e+04</td>\n",
       "      <td>68.730000</td>\n",
       "      <td>14.000000</td>\n",
       "      <td>16505.000000</td>\n",
       "      <td>48.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>535571.000000</td>\n",
       "      <td>53703.000000</td>\n",
       "      <td>7.092200e+04</td>\n",
       "      <td>76.670000</td>\n",
       "      <td>61.000000</td>\n",
       "      <td>20333.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>594571.000000</td>\n",
       "      <td>60247.000000</td>\n",
       "      <td>7.915900e+04</td>\n",
       "      <td>83.590000</td>\n",
       "      <td>130.000000</td>\n",
       "      <td>23000.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.818900e+04</td>\n",
       "      <td>6.720600e+04</td>\n",
       "      <td>6.508500e+04</td>\n",
       "      <td>2.094000e+03</td>\n",
       "      <td>1.26</td>\n",
       "      <td>2.500000e+01</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.670000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>671084.000000</td>\n",
       "      <td>990572.000000</td>\n",
       "      <td>1.628992e+06</td>\n",
       "      <td>95.000000</td>\n",
       "      <td>261.000000</td>\n",
       "      <td>24803.000000</td>\n",
       "      <td>156.000000</td>\n",
       "      <td>2018.0</td>\n",
       "      <td>22.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>9.652492e+07</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>2.564281e+07</td>\n",
       "      <td>inf</td>\n",
       "      <td>1.000000e+09</td>\n",
       "      <td>1.980000e+07</td>\n",
       "      <td>5.000000e+07</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>2.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                客户编号           已发货款          资产成本        贷款与资产比列  \\\n",
       "count  199717.000000  199717.000000  1.997170e+05  199717.000000   \n",
       "mean   535690.886665   54256.272280  7.582391e+04      74.643960   \n",
       "std     68193.411418   12977.656996  1.892894e+04      11.490485   \n",
       "min    417428.000000   13320.000000  3.700000e+04      10.030000   \n",
       "25%    476762.000000   46977.000000  6.571400e+04      68.730000   \n",
       "50%    535571.000000   53703.000000  7.092200e+04      76.670000   \n",
       "75%    594571.000000   60247.000000  7.915900e+04      83.590000   \n",
       "max    671084.000000  990572.000000  1.628992e+06      95.000000   \n",
       "\n",
       "                  品牌          骑车销售商             车厂      货款日期             地区  \\\n",
       "count  199717.000000  199717.000000  199717.000000  199717.0  199717.000000   \n",
       "mean       72.698508   19634.049665      69.085766    2018.0       7.245222   \n",
       "std        69.706185    3493.655400      22.128288       0.0       4.481338   \n",
       "min         1.000000   10524.000000      45.000000    2018.0       1.000000   \n",
       "25%        14.000000   16505.000000      48.000000    2018.0       4.000000   \n",
       "50%        61.000000   20333.000000      86.000000    2018.0       6.000000   \n",
       "75%       130.000000   23000.000000      86.000000    2018.0      10.000000   \n",
       "max       261.000000   24803.000000     156.000000    2018.0      22.000000   \n",
       "\n",
       "        是否填写手机号  ...    尚未还清有效贷款总额       已批准贷款总额       已发放贷款总额        每月还款总额  \\\n",
       "count  199717.0  ...  1.997170e+05  1.997170e+05  1.997170e+05  1.997170e+05   \n",
       "mean        1.0  ...  1.743125e+05  2.299233e+05  2.294165e+05  1.344553e+04   \n",
       "std         0.0  ...  9.813640e+05  2.530977e+06  2.534185e+06  1.531618e+05   \n",
       "min         1.0  ... -6.678296e+06  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "25%         1.0  ...  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "50%         1.0  ...  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00   \n",
       "75%         1.0  ...  3.818900e+04  6.720600e+04  6.508500e+04  2.094000e+03   \n",
       "max         1.0  ...  9.652492e+07  1.000000e+09  1.000000e+09  2.564281e+07   \n",
       "\n",
       "       贷款与已还贷款比列       主账户还款期数       次账户还款期数    贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  \\\n",
       "count  199717.00  1.997170e+05  1.997170e+05  1.997170e+05   199717.000000   \n",
       "mean         inf  5.059582e+04  2.928000e+03  5.535709e+02        1.438913   \n",
       "std          NaN  2.275670e+06  1.065410e+05  1.141343e+05        0.792213   \n",
       "min   -110000.33  0.000000e+00  0.000000e+00  0.000000e+00        1.000000   \n",
       "25%         1.00  0.000000e+00  0.000000e+00  1.000000e+00        1.000000   \n",
       "50%         1.00  0.000000e+00  0.000000e+00  1.000000e+00        1.000000   \n",
       "75%         1.26  2.500000e+01  0.000000e+00  1.000000e+00        1.670000   \n",
       "max          inf  1.000000e+09  1.980000e+07  5.000000e+07       18.000000   \n",
       "\n",
       "                工作类型  \n",
       "count  199717.000000  \n",
       "mean        0.487475  \n",
       "std         0.561915  \n",
       "min         0.000000  \n",
       "25%         0.000000  \n",
       "50%         0.000000  \n",
       "75%         1.000000  \n",
       "max         2.000000  \n",
       "\n",
       "[8 rows x 47 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#方法1\n",
    "data.info()\n",
    "#方法2\n",
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "7e7c0a69",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户编号              False\n",
       "已发货款              False\n",
       "资产成本              False\n",
       "贷款与资产比列           False\n",
       "品牌                False\n",
       "骑车销售商             False\n",
       "车厂                False\n",
       "货款日期              False\n",
       "地区                False\n",
       "是否填写手机号           False\n",
       "受否填写身份证           False\n",
       "是否出具驾驶证           False\n",
       "是否填写护照            False\n",
       "信用评分              False\n",
       "主账户贷款次数           False\n",
       "主账户有效贷款次数         False\n",
       "主账户中尚未还清有效贷款      False\n",
       "主账户中已批准的贷款        False\n",
       "主账户中已发放贷款         False\n",
       "次账户贷款次数           False\n",
       "次账户有效贷款次数         False\n",
       "次账户中尚未还清有效贷款      False\n",
       "次账户中已批准贷款         False\n",
       "次账户中已发放贷款         False\n",
       "主账户每月还款           False\n",
       "次账户没用还款           False\n",
       "近六个月新贷款次数         False\n",
       "近六个月违约次数          False\n",
       "平均贷款期限            False\n",
       "第一次贷款距今时间         False\n",
       "贷款查询次数            False\n",
       "是否违约              False\n",
       "贷款与资产比            False\n",
       "贷款总次数             False\n",
       "主账户无效贷款次数         False\n",
       "次账户无效贷款次数         False\n",
       "无效贷款总次数           False\n",
       "尚未还清有效贷款总额        False\n",
       "已批准贷款总额           False\n",
       "已发放贷款总额           False\n",
       "每月还款总额            False\n",
       "贷款与已还贷款比列         False\n",
       "主账户还款期数           False\n",
       "次账户还款期数           False\n",
       "贷款与已批准贷款比列        False\n",
       "总贷款次数与总有效贷款次数比    False\n",
       "工作类型              False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.isna().any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "50610575",
   "metadata": {},
   "outputs": [],
   "source": [
    "data.贷款与资产比[data.贷款与资产比 == np.inf]  =0 \n",
    "data.贷款与资产比[data.贷款与资产比.isna()] =0 \n",
    "\n",
    "data.贷款与已还贷款比列[data.贷款与已还贷款比列 == np.inf]  =0 \n",
    "data.贷款与已还贷款比列[data.贷款与已还贷款比列.isna()] =0 \n",
    "\n",
    "data.贷款与已批准贷款比列[data.贷款与已批准贷款比列 == np.inf]  =0 \n",
    "data.贷款与已批准贷款比列[data.贷款与已批准贷款比列.isna()] =0 \n",
    "\n",
    "data.总贷款次数与总有效贷款次数比 [data.总贷款次数与总有效贷款次数比  == np.inf]  =0 \n",
    "data.总贷款次数与总有效贷款次数比 [data.总贷款次数与总有效贷款次数比 .isna()] =0 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "8d5fb9c4",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "客户编号              0.0\n",
       "已发货款              0.0\n",
       "资产成本              0.0\n",
       "贷款与资产比列           0.0\n",
       "品牌                0.0\n",
       "骑车销售商             0.0\n",
       "车厂                0.0\n",
       "货款日期              0.0\n",
       "地区                0.0\n",
       "是否填写手机号           0.0\n",
       "受否填写身份证           0.0\n",
       "是否出具驾驶证           0.0\n",
       "是否填写护照            0.0\n",
       "信用评分              0.0\n",
       "主账户贷款次数           0.0\n",
       "主账户有效贷款次数         0.0\n",
       "主账户中尚未还清有效贷款      0.0\n",
       "主账户中已批准的贷款        0.0\n",
       "主账户中已发放贷款         0.0\n",
       "次账户贷款次数           0.0\n",
       "次账户有效贷款次数         0.0\n",
       "次账户中尚未还清有效贷款      0.0\n",
       "次账户中已批准贷款         0.0\n",
       "次账户中已发放贷款         0.0\n",
       "主账户每月还款           0.0\n",
       "次账户没用还款           0.0\n",
       "近六个月新贷款次数         0.0\n",
       "近六个月违约次数          0.0\n",
       "平均贷款期限            0.0\n",
       "第一次贷款距今时间         0.0\n",
       "贷款查询次数            0.0\n",
       "是否违约              0.0\n",
       "贷款与资产比            0.0\n",
       "贷款总次数             0.0\n",
       "主账户无效贷款次数         0.0\n",
       "次账户无效贷款次数         0.0\n",
       "无效贷款总次数           0.0\n",
       "尚未还清有效贷款总额        0.0\n",
       "已批准贷款总额           0.0\n",
       "已发放贷款总额           0.0\n",
       "每月还款总额            0.0\n",
       "贷款与已还贷款比列         0.0\n",
       "主账户还款期数           0.0\n",
       "次账户还款期数           0.0\n",
       "贷款与已批准贷款比列        0.0\n",
       "总贷款次数与总有效贷款次数比    0.0\n",
       "工作类型              0.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.apply(lambda x: x.isna().sum()/x.size,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca1650e8",
   "metadata": {},
   "source": [
    "## 衍生字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7992a6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#      1. 还款意愿 ， 刻画（逾期次数，最大逾期天数，平均逾期天数,违约次数，芝麻分数，征信）\n",
    "#      2. 还款能力，  贷款收入比，平均收入，收入离散系数，"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cac760c",
   "metadata": {},
   "source": [
    "## 失衡数据判断并处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "af621bc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    164289\n",
       "1     35428\n",
       "Name: 是否违约, dtype: int64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.是否违约.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "a30db94b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.17739100827671156"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "35428/data.是否违约.size  # 百分之十七"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98861584",
   "metadata": {},
   "source": [
    "## 多个备选模型比较"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "26d66475",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>客户编号</th>\n",
       "      <th>已发货款</th>\n",
       "      <th>资产成本</th>\n",
       "      <th>贷款与资产比列</th>\n",
       "      <th>品牌</th>\n",
       "      <th>骑车销售商</th>\n",
       "      <th>车厂</th>\n",
       "      <th>货款日期</th>\n",
       "      <th>地区</th>\n",
       "      <th>是否填写手机号</th>\n",
       "      <th>...</th>\n",
       "      <th>尚未还清有效贷款总额</th>\n",
       "      <th>已批准贷款总额</th>\n",
       "      <th>已发放贷款总额</th>\n",
       "      <th>每月还款总额</th>\n",
       "      <th>贷款与已还贷款比列</th>\n",
       "      <th>主账户还款期数</th>\n",
       "      <th>次账户还款期数</th>\n",
       "      <th>贷款与已批准贷款比列</th>\n",
       "      <th>总贷款次数与总有效贷款次数比</th>\n",
       "      <th>工作类型</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601758</td>\n",
       "      <td>65532</td>\n",
       "      <td>78990</td>\n",
       "      <td>84.379997</td>\n",
       "      <td>136</td>\n",
       "      <td>20490</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>519488</td>\n",
       "      <td>56759</td>\n",
       "      <td>65325</td>\n",
       "      <td>89.550003</td>\n",
       "      <td>61</td>\n",
       "      <td>22778</td>\n",
       "      <td>86</td>\n",
       "      <td>2018</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>2054139</td>\n",
       "      <td>2036500</td>\n",
       "      <td>2036500</td>\n",
       "      <td>34455</td>\n",
       "      <td>0.99</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.33</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>447579</td>\n",
       "      <td>58413</td>\n",
       "      <td>67960</td>\n",
       "      <td>89.019997</td>\n",
       "      <td>5</td>\n",
       "      <td>15663</td>\n",
       "      <td>86</td>\n",
       "      <td>2018</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>648134</td>\n",
       "      <td>72317</td>\n",
       "      <td>99750</td>\n",
       "      <td>73.680000</td>\n",
       "      <td>76</td>\n",
       "      <td>17242</td>\n",
       "      <td>48</td>\n",
       "      <td>2018</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>13813</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>13814.00</td>\n",
       "      <td>13813</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.00</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>458210</td>\n",
       "      <td>50078</td>\n",
       "      <td>65450</td>\n",
       "      <td>79.449997</td>\n",
       "      <td>146</td>\n",
       "      <td>14181</td>\n",
       "      <td>45</td>\n",
       "      <td>2018</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>467161</td>\n",
       "      <td>550000</td>\n",
       "      <td>550000</td>\n",
       "      <td>12863</td>\n",
       "      <td>1.18</td>\n",
       "      <td>42</td>\n",
       "      <td>0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.06</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 47 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     客户编号   已发货款   资产成本    贷款与资产比列   品牌  骑车销售商  车厂  货款日期  地区  是否填写手机号  ...  \\\n",
       "0  601758  65532  78990  84.379997  136  20490  45  2018   8        1  ...   \n",
       "1  519488  56759  65325  89.550003   61  22778  86  2018   6        1  ...   \n",
       "2  447579  58413  67960  89.019997    5  15663  86  2018   9        1  ...   \n",
       "3  648134  72317  99750  73.680000   76  17242  48  2018   8        1  ...   \n",
       "4  458210  50078  65450  79.449997  146  14181  45  2018  17        1  ...   \n",
       "\n",
       "   尚未还清有效贷款总额  已批准贷款总额  已发放贷款总额  每月还款总额  贷款与已还贷款比列  主账户还款期数  次账户还款期数  \\\n",
       "0           0        0        0       0       1.00        0        0   \n",
       "1     2054139  2036500  2036500   34455       0.99       59        0   \n",
       "2           0        0        0       0       1.00        0        0   \n",
       "3           0    13813    13813       0   13814.00    13813        0   \n",
       "4      467161   550000   550000   12863       1.18       42        0   \n",
       "\n",
       "   贷款与已批准贷款比列  总贷款次数与总有效贷款次数比  工作类型  \n",
       "0         1.0            1.00     0  \n",
       "1         1.0            1.33     1  \n",
       "2         1.0            1.00     1  \n",
       "3         1.0            2.00     0  \n",
       "4         1.0            1.06     1  \n",
       "\n",
       "[5 rows x 47 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "3310d8d5",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 199717 entries, 0 to 199716\n",
      "Data columns (total 44 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   已发货款            199717 non-null  float32\n",
      " 1   资产成本            199717 non-null  float32\n",
      " 2   贷款与资产比列         199717 non-null  float32\n",
      " 3   品牌              199717 non-null  float32\n",
      " 4   骑车销售商           199717 non-null  float32\n",
      " 5   车厂              199717 non-null  float32\n",
      " 6   货款日期            199717 non-null  float32\n",
      " 7   地区              199717 non-null  float32\n",
      " 8   是否出具驾驶证         199717 non-null  float32\n",
      " 9   是否填写护照          199717 non-null  float32\n",
      " 10  信用评分            199717 non-null  float32\n",
      " 11  主账户贷款次数         199717 non-null  float32\n",
      " 12  主账户有效贷款次数       199717 non-null  float32\n",
      " 13  主账户中尚未还清有效贷款    199717 non-null  float32\n",
      " 14  主账户中已批准的贷款      199717 non-null  float32\n",
      " 15  主账户中已发放贷款       199717 non-null  float32\n",
      " 16  次账户贷款次数         199717 non-null  float32\n",
      " 17  次账户有效贷款次数       199717 non-null  float32\n",
      " 18  次账户中尚未还清有效贷款    199717 non-null  float32\n",
      " 19  次账户中已批准贷款       199717 non-null  float32\n",
      " 20  次账户中已发放贷款       199717 non-null  float32\n",
      " 21  主账户每月还款         199717 non-null  float32\n",
      " 22  次账户没用还款         199717 non-null  float32\n",
      " 23  近六个月新贷款次数       199717 non-null  float32\n",
      " 24  近六个月违约次数        199717 non-null  float32\n",
      " 25  平均贷款期限          199717 non-null  float32\n",
      " 26  第一次贷款距今时间       199717 non-null  float32\n",
      " 27  贷款查询次数          199717 non-null  float32\n",
      " 28  是否违约            199717 non-null  float32\n",
      " 29  贷款与资产比          199717 non-null  float32\n",
      " 30  贷款总次数           199717 non-null  float32\n",
      " 31  主账户无效贷款次数       199717 non-null  float32\n",
      " 32  次账户无效贷款次数       199717 non-null  float32\n",
      " 33  无效贷款总次数         199717 non-null  float32\n",
      " 34  尚未还清有效贷款总额      199717 non-null  float32\n",
      " 35  已批准贷款总额         199717 non-null  float32\n",
      " 36  已发放贷款总额         199717 non-null  float32\n",
      " 37  每月还款总额          199717 non-null  float32\n",
      " 38  贷款与已还贷款比列       199717 non-null  float32\n",
      " 39  主账户还款期数         199717 non-null  float32\n",
      " 40  次账户还款期数         199717 non-null  float32\n",
      " 41  贷款与已批准贷款比列      199717 non-null  float32\n",
      " 42  总贷款次数与总有效贷款次数比  199717 non-null  float32\n",
      " 43  工作类型            199717 non-null  float32\n",
      "dtypes: float32(44)\n",
      "memory usage: 33.5 MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "feab84f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "\n",
    "# 模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 集成学习和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier\n",
    "import xgboost as xgb\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "# 评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score, classification_report\n",
    "\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "3720d1e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in data.columns:\n",
    "    if data[i].dtype == np.int64 or np.float64:\n",
    "        data[i] = data[i].astype(\"float32\") #改字段类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "07d78a4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(data.iloc[:,1:],data.是否违约,test_size=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "0f9ac879",
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(X_train, y_train, X_test, y_test, model,model_name):\n",
    "    \n",
    "    print('训练{}'.format(model_name))\n",
    "    \n",
    "    #创建指定模型\n",
    "    clf=model \n",
    "    start = time.time()\n",
    "    \n",
    "    #训练模型\n",
    "    clf.fit(X_train, y_train.values.ravel())\n",
    "    \n",
    "    #验证模型\n",
    "    print(\"训练集评估\")\n",
    "    train_pre = clf.predict(X_train) \n",
    "    print(classification_report(y_train,train_pre))\n",
    "    \n",
    "    print(\"检验集评估\")\n",
    "    test_pre=clf.predict(X_test)\n",
    "    print(classification_report(y_test,test_pre))\n",
    "\n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "\n",
    "    return clf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "c95ab192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练LR\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       0.82      1.00      0.90    115102\n",
      "         1.0       0.24      0.00      0.00     24699\n",
      "\n",
      "    accuracy                           0.82    139801\n",
      "   macro avg       0.53      0.50      0.45    139801\n",
      "weighted avg       0.72      0.82      0.74    139801\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       0.82      1.00      0.90     49187\n",
      "         1.0       0.00      0.00      0.00     10729\n",
      "\n",
      "    accuracy                           0.82     59916\n",
      "   macro avg       0.41      0.50      0.45     59916\n",
      "weighted avg       0.67      0.82      0.74     59916\n",
      "\n",
      "模型训练耗时：1.759937s\n",
      "训练DT\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00    115102\n",
      "         1.0       1.00      1.00      1.00     24699\n",
      "\n",
      "    accuracy                           1.00    139801\n",
      "   macro avg       1.00      1.00      1.00    139801\n",
      "weighted avg       1.00      1.00      1.00    139801\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00     49187\n",
      "         1.0       1.00      1.00      1.00     10729\n",
      "\n",
      "    accuracy                           1.00     59916\n",
      "   macro avg       1.00      1.00      1.00     59916\n",
      "weighted avg       1.00      1.00      1.00     59916\n",
      "\n",
      "模型训练耗时：0.470162s\n",
      "训练AdaBoost\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00    115102\n",
      "         1.0       1.00      1.00      1.00     24699\n",
      "\n",
      "    accuracy                           1.00    139801\n",
      "   macro avg       1.00      1.00      1.00    139801\n",
      "weighted avg       1.00      1.00      1.00    139801\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00     49187\n",
      "         1.0       1.00      1.00      1.00     10729\n",
      "\n",
      "    accuracy                           1.00     59916\n",
      "   macro avg       1.00      1.00      1.00     59916\n",
      "weighted avg       1.00      1.00      1.00     59916\n",
      "\n",
      "模型训练耗时：0.552320s\n",
      "训练RF\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00    115102\n",
      "         1.0       1.00      1.00      1.00     24699\n",
      "\n",
      "    accuracy                           1.00    139801\n",
      "   macro avg       1.00      1.00      1.00    139801\n",
      "weighted avg       1.00      1.00      1.00    139801\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00     49187\n",
      "         1.0       1.00      1.00      1.00     10729\n",
      "\n",
      "    accuracy                           1.00     59916\n",
      "   macro avg       1.00      1.00      1.00     59916\n",
      "weighted avg       1.00      1.00      1.00     59916\n",
      "\n",
      "模型训练耗时：11.214956s\n",
      "训练XGBoost\n",
      "[19:29:19] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.4.0/src/learner.cc:1095: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "训练集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00    115102\n",
      "         1.0       1.00      1.00      1.00     24699\n",
      "\n",
      "    accuracy                           1.00    139801\n",
      "   macro avg       1.00      1.00      1.00    139801\n",
      "weighted avg       1.00      1.00      1.00    139801\n",
      "\n",
      "检验集评估\n",
      "              precision    recall  f1-score   support\n",
      "\n",
      "         0.0       1.00      1.00      1.00     49187\n",
      "         1.0       1.00      1.00      1.00     10729\n",
      "\n",
      "    accuracy                           1.00     59916\n",
      "   macro avg       1.00      1.00      1.00     59916\n",
      "weighted avg       1.00      1.00      1.00     59916\n",
      "\n",
      "模型训练耗时：3.088288s\n"
     ]
    }
   ],
   "source": [
    "model_name_param_dict = { 'LR':(LogisticRegression()),\n",
    "                          'DT': (DecisionTreeClassifier()),\n",
    "                          'AdaBoost': (AdaBoostClassifier()),\n",
    "                          'RF': (RandomForestClassifier()),\n",
    "                          'XGBoost':(XGBClassifier())\n",
    "                         }\n",
    "result = {}\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    result[model_name] = train_model(X_train, y_train, X_test, y_test, model,model_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "58ba9f8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(139801, 43)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b871764b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "param_grid = {'n_estimators': [20, 50, 100,300],\"max_depth\":[4,6,8,10,12],\n",
    "             \"criterion\": [\"gini\", \"entropy\"],\"max_features\": [10,15,20,5],},\n",
    "model = RandomForestClassifier()\n",
    "grid_search = GridSearchCV(model, param_grid, cv=3, scoring='roc_auc')\n",
    "temp=grid_search.fit(X_train, y_train)\n",
    "temp.best_params_"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "983aac64",
   "metadata": {},
   "source": [
    "## 优质模型保存"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29a16146",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = RandomForestClassifier(n_estimators =,max_depth=,criterion=,max_features=)\n",
    "train_model(X_train, y_train, X_test, y_test, model,\"随机森林\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56b7d30e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.externals import joblib\n",
    "#保存模型\n",
    "joblib.dump(temp,'model.pkl')\n",
    "\n",
    "#加载模型\n",
    "#clf=joblib.load('model.model')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3d18554",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
